<?php
namespace src\logic;
use \classlib\core as C;
class lsetting {
	public static function submit($view, $userid, $title, $tagids, $airtime=0, $isfb=0) {
		$lang = C\lang::inst('web');
		$nlen = mb_strlen($title, 'utf-8');
		if ($nlen > 70 || $nlen < 1) {
			$view->assign_error(80050, $lang->txt('action', '80050'));
		}
		$res  = limapi::filter(2, $title);//验证屏蔽关键词
		if ($res && $res['ResultContent'] == 1) {
			$err_title = $lang->txt('filter', 'title');
			$view->assign_error(80050, $lang->txt('action', '80088', $title));
		}
		if (empty($tagids)) {
			$view->assign_error(80051, $lang->txt('action', '80051'));
		}
		//不存在直播间直接开通直播间
		$studio  = lstudio::get($userid, -1);
		if (!$studio) {
			$user= C\model::load('main.pl.userinfo')->set_dev($userid)->get_one($userid);
			if (!$user['emailverify']) {
				$view->assign_error(80114, $lang->txt('action', '80114'));
			}
			$studio = lstudio::open($userid, $user);
		} else if ($studio['status'] != 1) {
			$view->assign_error(80115, $lang->txt('action', '80115'));
		}
		//设置标签资料信息
		$fields  = array('title'=>$title, 'isset'=>0, 'mshare'=>$isfb, 'stime'=>C\config::$nowtime);
		if ($studio && $studio['isshow'] == 0) {
			$fields['isshow'] = ($studio['origin'] == 0)? 2:1;
		}
		$oldtags = (isset($studio['tags']) && !empty($studio['tags']))? explode(',', $studio['tags']):array();
		$fields['tags'] = ltags::build($studio['id'], $oldtags, $tagids);
		//预约时间不一致的话要重置之前的预约时间
		if ($airtime > 0) {
			if ($studio['airtime'] > C\config::$nowtime && $studio['airtime'] != $airtime) {
				levent::airNotify($studio, $airtime);
			}
			$fields['airtime'] = $airtime;
		}
		//获取标签对象 set到studio当中
		C\model::load('main.pl.studio')->save($studio['id'], $fields);
	}
	
	//上传图片的时候自动截图
	public static function thumb($view, $userid, $origin, $subfix=false) {
		$lang   = C\lang::inst('web');
		$studio = lstudio::get($userid);
		if (!$studio) {
			$view->assign_error(80009, $lang->txt('action', '80009'));
		}
		if (!$subfix) {
			$ctype  = isset($_SERVER['CONTENT_TYPE'])? $_SERVER['CONTENT_TYPE']:'';
			if (!$ctype || strcasecmp(substr($ctype, 0, 6), 'image/') != 0) {
				$view->assign_error(80052, $lang->txt('action', '80052'));
			}
			$subfix = strtolower(substr($ctype, 6));
		}
		if (!$origin || strlen($origin) > lshare::MAXSIZE) {
			$view->assign_error(80052, $lang->txt('action', '80052'));
		}
		$url    = lupfile::dostream($view, $origin, $subfix, $userid, 'thumb');
		if (!$url) {
			$view->assign_error(80054, $lang->txt('action', '80054'));
		}
		//获取标签对象 set到studio当中
		$fields = array('thumb'=>$url, 'stime'=>C\config::$nowtime, 'isset'=>1);
		C\model::load('main.pl.studio')->save($studio['id'], $fields);
		lupfile::thumbnail($url, $userid, 'thumb');
		if ($studio['mshare']) {
			//刷新数据到浏览器然后停止
			(function_exists('fastcgi_finish_request')) && fastcgi_finish_request();
			ignore_user_abort(true);
			ob_start();
			/*
			try {
				require_once WEBROOT.'/controller/thirdlogin/facebook.do.php';
				$fb   = new Cfacebook();
				$link = router::get_url('live', $studio['studiono']);
				$fb->feed($studio['title'], static_uri().$url, $link);
			} catch(Exception $e) {
				//
			}
			*/
			ob_end_clean();
		}
		//api_fbgraph::clear($link);
		return $url;
	}
}